<?php
if($levelTL != 3) {
	die("Bạn không đủ quyền để truy cập trang này!");
}
if($mode == 'delete') {
	$db->dbdelete("user","userid = '$ipid'");
}
if($mode == 'edit') {
	$user	=	$db->database("password,usergroup,username,email,usercode,gold","user","userid = '$ipid'");	
		$password		=	$user[0][0];
		$username		=	$user[0][2];
		$email			=	$user[0][3];
		$usergroup		=	$user[0][1];
		$usercode		=	$user[0][4];
		$gold			=	$user[0][5];
	if($_POST['submit']) {
		$zpassword		=	htmlchars(stripslashes(trim(urldecode($_POST['password']))));
		$username		=	htmlchars(stripslashes(trim(urldecode($_POST['username']))));
		$email			=	htmlchars(stripslashes(trim(urldecode($_POST['email']))));
		$usergroup		=	htmlchars(stripslashes(trim(urldecode($_POST['usergroup']))));
		$gold			=	htmlchars(stripslashes(trim(urldecode($_POST['gold']))));
		if($username && $email) {
			if($zpassword) {
				$password	=	md5(md5($zpassword).$usercode);
			}
			$db->dbupdate("user","password='$password',
									  			username='$username',
												email='$email',
												usergroup='$usergroup',
												gold	=	'$gold'","userid = '$ipid'");
			header("Location: index.php?ipos=user");
		}
	}
?>
<div class="content_block">
		<div id="slides" class="form_tbl">	
				<span class="border_d clearfix">
					<form method="post">
					<h1 style="padding: 16px 0px 16px 0px;">Sửa thành viên: <strong>#<?=$ipid;?></strong></h1>
						<div class="form_item">
							<label>Tên tài khoản:</label>
							<input maxlength="250" value="<?=$username;?>" name="username" size="30" class="flat">
						</div>
						<div class="form_item">
							<label>Mật khẩu:</label>
							<input maxlength="250" name="password" size="30" class="flat"> (Không đổi để trống)
						</div>
						<div class="form_item">
							<label>Email:</label>
							<input maxlength="250" value="<?=$email;?>" name="email" size="30" class="flat">
						</div>
						<div class="form_item">
							<label>Nhóm:</label>
							<select name="usergroup">
                                        <? 
										for($i=0;$i<4;$i++) { 
											if($i == $usergroup)	$cssof[$i]	=	"selected";
										?>
                                            <option value="<?=$i?>" <?=$cssof[$i]?>><?=groupName($i)?></option>
                                        <? } ?>
                            </select>
						</div>
						<div class="form_item">
							<label><?=GOLD;?>:</label>
							<input maxlength="250" value="<?=$gold;?>" name="gold" size="30" class="flat">
						</div>
 						<div class="form_item">
							<span class="mb_btn1" onmouseover="this.className='mb_btn1_hover'" onmouseout="this.className='mb_btn1'">
								<button type="submit" name="submit" value="Submit">Gửi đi</button>
							</span>
						</div>
</form>
				</span>

			</div>
		</div>
<?	
}else {
	?>

	<div class="content_block">
		<div class="form_tbl" id="slides">
				<span class="border_d clearfix">
<h1 style="padding: 16px 0px 16px 0px;">Danh sách thành viên</h1>
<table cellspacing="0" cellpadding="0" width="100%">
                	<tbody><tr>
                        <td align="center" class="form_item">
                        User ID thành viên <input type="text" id="userid" style="width:50px;"> <input type="submit" value="Sửa" onclick="window.location.href = 'index.php?ipos=user&amp;mode=edit&amp;ipid='+document.getElementById('userid').value;" style="width:50px;cursor:pointer;">
                        </td>
                    </tr>
                </tbody></table>
                		<table cellpadding="0" cellspacing="0" width="100%">
                        	<tr>
                            <td class="form_item" width="40" align="center">User ID</td>
                            <td class="form_item" align="left">Username</td>
                            <td class="form_item" align="center">Email</td>
                            <td class="form_item" align="center">Nhóm</td>
                            <td class="form_item" width="120" align="center"></td>
                            </tr>
<?php
$num		= FILM_PAGE_ADMIN;
$num 		= intval($num);
$page 		= intval($page);
if (!$page) $page = 1;
$limit 		= ($page-1)*$num;
if($limit<0) $limit=0;
$arr		=	$db->database("userid,usergroup,username,email","user","userid>0 order by userid desc limit $limit,$num");
$total		=	$db->database("userid","user","userid>0 order by userid desc");
$total		=	count($total);
$pagehtml 	= 	ipos_pages($total,$num,$page,"index.php?ipos=user&p=");
for($i=0;$i<count($arr);$i++) {
?>
                        	<tr>
                            <td class="form_item" width="10" align="center"><?=$arr[$i][0];?></td>
                            <td class="form_item" align="left"><?=$arr[$i][2];?></td>
                            <td class="form_item" align="center"><?=$arr[$i][3];?></td>
                            <td class="form_item" align="center"><?=groupName($arr[$i][1]);?></td>
                            <td class="form_item" width="120" align="center"><a href="index.php?ipos=user&mode=edit&ipid=<?=$arr[$i][0];?>">Sửa</a> / <a href="index.php?ipos=user&mode=delete&ipid=<?=$arr[$i][0];?>"  onClick="return ask()">Xóa</a></td>
                            </tr>
<? } ?>
<tr><td class="form_item" colspan="5" style="text-align: right;"><ul class="pages"><?=$pagehtml;?></ul></td></tr>
                        </table>
				</span>

			</div>
		</div>
<script>
function ask(){
	if (confirm("Bạn có chắc chắn muốn xóa thành viên này!")) return true;
	return false ;
}
</script>
<? } ?>